﻿using BXH.Movie.Bll;
using BXH.Movie.Common;
using BXH.Movie.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BXH.Movie
{
    public partial class FrmRegisterAndAddAdmin : CCWin.CCSkinMain
    {
        private UserInfoService userInfoService = new UserInfoService();
        private int roleType = RoleType.User;
        public event EventHandler AddSuccess;
        public FrmRegisterAndAddAdmin(int roleType)
        {
        
            InitializeComponent();
            txtAge.Text = string.Empty;
            txtName.Text = string.Empty;
            txtNick.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtRpassword.Text = string.Empty;
            txtUserCode.Text = string.Empty;
            this.StartPosition = FormStartPosition.CenterParent;
            this.roleType = roleType;
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text != txtRpassword.Text)
            {

                MessageBox.Show("两次输入的密码不一样");
                return;
            }

            UserInfo userInfo = new UserInfo();
            userInfo.UserCode = txtUserCode.Text.Trim();

            var encryptPassword = Encrypt.MD5Encrypt16(txtPassword.Text);
            userInfo.Password = encryptPassword;
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
                userInfo.UserName = txtName.Text.Trim();
            if (!string.IsNullOrEmpty(txtNick.Text.Trim()))
                userInfo.UserNick = txtNick.Text.Trim();
            if (!string.IsNullOrEmpty(txtAge.Text.Trim()))
                userInfo.Age = int.Parse(txtAge.Text.Trim());
            //if (!string.IsNullOrEmpty(cbxSex.Text.Trim()))
            userInfo.Sex = rdoSexNan.Checked ? 1 : 0;
            userInfo.RoleId = roleType;
            switch (userInfo.RoleId) {

                case RoleType.User:
                    if (userInfoService.Reigster(userInfo))
                    {
                        AddSuccess?.Invoke(null, null);
                        MessageBox.Show("注册成功");
                        this.Close();
                    }
                    break;
                case RoleType.Admin:
                    if (userInfoService.AddAdmin(userInfo))
                    {
                        AddSuccess?.Invoke(null,null);
                        MessageBox.Show("添加成功");
                        this.Close();
                    }
                    break;

            }
           


        }
    }
}
